package com.chinasofti.framework.base;

import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.chinasofti.framework.data.Entity;


public class Utils {
	public static Date Now() {
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		return date;
	}
	
	public static Date ParseDate(String dateString) {
		String[] array = dateString.split(" ");
		String strDate = null;
		String strTime = null;
		if(array.length > 0) {
			strDate = array[0];
			strTime = "00:00:00";
		}
		if(array.length == 2) {
			strTime = array[1];
		}
		
		strDate = strDate.replace('.', '-');
		strDate = strDate.replace('/', '-');
		
		strTime = strTime.replace('.', ':');
		String strDateTime = strDate + " " + strTime;
		return ParseDate(strDateTime, "yyyy-MM-dd hh:mm:ss");
	}
	
	public static Date ParseDate(String dateString, String formatString) {
		java.text.SimpleDateFormat sdf = new SimpleDateFormat(
				formatString,java.util.Locale.CHINA); 
		try {
			return sdf.parse(dateString);
		} 
		catch (ParseException e) {
			return null;
		}
	}
	
	public static String LocalDateString(Date date) {
		return Utils.LocalDateString(date, "yyyy-MM-dd hh:mm:ss");
	}
	
	@SuppressWarnings("rawtypes")
	public static boolean isDerived(Class baseClass, Class derivedClass) {
	
		Class superClass = derivedClass.getSuperclass();
		while(superClass != null) {
			if(baseClass.getName().equals(superClass.getName())){
				return true;
			}
			superClass = superClass.getSuperclass();
		}
		return false;
	}
	
	public static String getSimpleString(Object o) {
		Type type = o.getClass();
		if(type.equals(Type.class) || type.equals(Class.class)) {
			return type.getClass().getName();
		}
		else if(type.equals(Date.class)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String value = sdf.format(o);
			return value;
		}
		else {
			return o.toString();
		}
	}
	
	public static <T extends Entity> String toXmlString(ArrayList<T> entities, String tag) {
		StringBuilder xml = new StringBuilder();
		for(int i = 0; i < entities.size(); i++) {
			T entity = entities.get(i);
			String name = entity.getEntityName();
			xml.append(entity.toXmlString(name));
		}
		return "<" + tag + ">" + xml.toString() + "</" + tag + ">";
	}
	
	public static boolean instanceOf(Type type, Object object) {
		if(object == null) {
			return true;
		}
		Class<?> typeClass = (Class<?>)type;
		Class<?> objectClass = object.getClass();
		String typeClassName = typeClass.getName();
		String objectClassName = objectClass.getName();
		if((typeClassName.equals("java.lang.reflect.Type") || typeClassName.equals("java.lang.Class")) &&
				(objectClassName.equals("java.lang.reflect.Type") ||objectClassName.equals("java.lang.Class")) ) {
			return true;
		}
		else if(typeClass.equals(objectClass)) {
			return true;
		}
		else if(Utils.isDerived(typeClass, objectClass)) {
			return true;
		}
		return false;
	}
	
	public static String UrlEncode(String url) {
		return URLEncoder.encode(url);
	}

	public static String LocalDateString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
}
